package com.example.demo;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author chenw
 * @date 2021/8/25
 */
@CacheConfig(cacheNames = "caffeineCacheManager")
@RestController
@RequestMapping("")
public class UserDemoController {
    @GetMapping("getUser")
    @Cacheable(value = "userDto",key = "#id")
    public UserDto getUser(Integer id){
        System.out.println("从数据库查");
        return new UserDto(1,"chenw");
    }
    /**
     * 新增或修改用户信息
     *
     * @param id
     */
    @GetMapping("/saveUser")
    @CachePut(key = "#id")
    public UserDto saveUser(Integer id) {
        return  new UserDto(id,"chenw");
    }

    /**
     * 删除用户信息
     *
     * @param id
     */
    @GetMapping("/removeUser")
    @CacheEvict(value = "userDto",key = "#id")
    public Boolean removeUser(Integer id){
        return true;
    }
}
